package com.byit.controller;

import cn.jiguang.common.utils.StringUtils;
import com.byit.dto.ProCreateOrderDto;
import com.byit.service.BestPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by Administrator on 2019/1/25.
 */
@RestController
@RequestMapping("/app/pay")
public class AppPayController {
    @Autowired
    private BestPayService bestPayService;


    /**
     * 下单并支付
     * @param proCreateOrderDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/getUrl")
    public Object getUrl(ProCreateOrderDto proCreateOrderDto) throws Exception {
        String url = bestPayService.getUrl(proCreateOrderDto);

        ModelAndView view = null;
        if(StringUtils.isNotEmpty(url)) {
            view = new ModelAndView("redirect:" + url);
        }
        return view;
    }

    /**
     * 退款
     * @param outRequestNo 退款单号
     * @param outTradeNo 需要退款的订单号
     * @return
     * @throws IOException
     */
    @RequestMapping("/refund")
    public Object proCreateOrder(String outRequestNo,String outTradeNo,String refundAmt,String originalTradeDate) {
        Object o = null;
        try {
            o = bestPayService.refund(outRequestNo,outTradeNo,refundAmt,originalTradeDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return o;
    }

    /**
     * 支付回调
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/callbackPay")
    public Object callbackPay(HttpServletRequest request) {
        Object o = null;
        try {
            o = bestPayService.callbackPay(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return o;
    }

    /**
     * 退款回调
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/callBackRefund")
    public Object callBackRefund(HttpServletRequest request) {
        Object o = null;
        try {
            o = bestPayService.callBackRefund(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return o;
    }
}
